//
//  LadingCarGoodListVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/14.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LadingCarGoodListVC.h"
#import "GoodListCell.h"
#import "GoodListSectionHeadView.h"
#import "LJInPutTool.h"

@interface LadingCarGoodListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) LJInPutTool *inPutTool;//输入框

@end

@implementation LadingCarGoodListVC

-(LJInPutTool *)inPutTool {
    if (!_inPutTool) {
        _inPutTool = [LJInPutTool new];
        __weak_self;
        _inPutTool.finishBlock = ^(NSString * _Nonnull str) {
            [weakself boHuiBill:str];
        };
    }
    return _inPutTool;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

-(NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSArray array];
    }
    return _titleArray;
}

-(NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight)];
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, self.scrollView.nim_height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"装车清单";
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest GetWithApi:ReceiptDeliveryTiHuoCarGoodList params:@{@"id":self.Id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.titleArray = [responseObject objectForKey:@"header"];
            [self.array removeAllObjects];
            self.array = [NSMutableArray arrayWithArray:responseObject[@"list"]];
            NSString *status = [responseObject objectForKey:@"status"];
            [self configFrame:status];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)configFrame:(NSString *)status {
    if ([status isEqualToString:@"1"]) {
        [self.view addSubview:self.bottomView];
        UIButton *rejectBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [rejectBtn setBackgroundColor:HexColor(0xF35959)];
        rejectBtn.layer.masksToBounds = YES;
        rejectBtn.layer.cornerRadius = 6;
        [rejectBtn setImage:[UIImage imageNamed:@"icon_reject"] forState:0];
        [rejectBtn setTitle:@"驳回" forState:0];
        [rejectBtn setTitleColor:HexColor(0xffffff) forState:0];
        rejectBtn.titleLabel.font = BoldFontOfSize(16);
        [rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
        rejectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:rejectBtn];
        
        UIButton *passBtn = [[UIButton alloc] initWithFrame:CGRectMake(rejectBtn.nim_right + 15, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [passBtn setBackgroundColor:Color_ThemeColor];
        passBtn.layer.masksToBounds = YES;
        passBtn.layer.cornerRadius = 6;
        [passBtn setImage:[UIImage imageNamed:@"icon_adopt"] forState:0];
        [passBtn setTitle:@"通过" forState:0];
        [passBtn setTitleColor:HexColor(0xffffff) forState:0];
        passBtn.titleLabel.font = BoldFontOfSize(16);
        [passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
        passBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:passBtn];
        self.scrollView.nim_height = ScreenH - NaviHeight - self.bottomView.nim_height;
        self.tableView.nim_height = self.scrollView.nim_height;
    }else {
        [self.bottomView removeFromSuperview];
        self.scrollView.nim_height = ScreenH - NaviHeight - BottomHeight;
        self.tableView.nim_height = self.scrollView.nim_height;
    }
    
    NSArray *dic = self.array.firstObject;
    self.scrollView.contentSize = CGSizeMake(GoodList_ViewWidth * dic.count, self.scrollView.bounds.size.height);
    self.tableView.nim_width = self.scrollView.contentSize.width;
}

- (void)rejectClick {
    self.inPutTool.placeholder = @"请输入驳回原因";
    self.inPutTool.sureBtnText = @"确认驳回";
    self.inPutTool.cancleBtnText = @"取消驳回";
    [self.inPutTool showInView:MAIN_WINDOW];
}

- (void)passClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定通过吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sureBill];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)sureBill {
    [HttpRequest PostWithApi:ReceiptDeliveryTiHuoGoodListOperation params:@{@"id":self.Id,@"rejectReason":@"",@"type":@"1"} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已通过"];
            [self requestForData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)boHuiBill:(NSString *)str {
    [HttpRequest PostWithApi:ReceiptDeliveryTiHuoGoodListOperation params:@{@"id":self.Id,@"rejectReason":str,@"type":@"2"} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"驳回成功"];
            [self requestForData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GoodListSectionHeadView *view = [[GoodListSectionHeadView alloc] initWithFrame:CGRectMake(0, 0, GoodList_ViewWidth * self.titleArray.count, 45)];
    view.titleArray = self.titleArray;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodListCell *cell = [GoodListCell GoodListCellWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodListCell *cell = [GoodListCell GoodListCellWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    return cell.height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
